﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mjRegister.Entity;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;

namespace mjRegister.BAL
{
    public class Team
    {
        public static string Oil(string verify, int teamuid, int teamid, OilType oilType)
        {
            try
            {
                string postData = string.Format("verify={0}&teamuid={1}&teamid={2}&type={3}",
                    verify, teamuid.ToString(), teamid.ToString(), ((int)oilType).ToString());
                string url = "http://www.kaixin001.com/parking/teamoil.php";
                HttpProc hp = new HttpProc(url, postData);
                HttpWebRequest request = hp.CreateRequest();
                request.AllowAutoRedirect = false;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if ((int)response.StatusCode == 302)
                {
                    string location = response.Headers["location"];
                    response.Close();
                    url = string.Format("http://www.kaixin001.com/parking/teamoil.php{0}", location);
                    hp = new HttpProc(url);
                    string ResStr1 = hp.Proc();
                    string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                    RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);
                    if (Regex.IsMatch(ResStr1, re, options))
                    {
                        Match mc = Regex.Match(ResStr1, re, options);
                        string matchcontent = mc.Value;
                        return Regex.Replace(Common.FilterHtml(matchcontent), "(\n|\t|\r)", "").Replace("</div", string.Empty);
                        //string re1 = "<strong>(?<message>.*?)</strong>";
                        //return Regex.Match(matchcontent, re1).Result("${message}").Replace("<br />", "\n");
                    }
                }
                else if (response.StatusCode == HttpStatusCode.OK)
                {
                    StreamReader sr = null;
                    try
                    {
                        sr = new StreamReader(response.GetResponseStream(), hp.encoding);
                        string ResStr1 = sr.ReadToEnd();
                        string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                        RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);
                        if (Regex.IsMatch(ResStr1, re, options))
                        {
                            Match mc = Regex.Match(ResStr1, re, options);
                            string matchcontent = mc.Value;
                            string re1 = "<strong>(?<message>.*?)</strong>";
                            return Regex.Replace(Common.FilterHtml(Regex.Match(matchcontent, re1, options).Result("${message}")), "(\n|\t|\r)", "");
                        }
                        else
                            return string.Empty;

                    }
                    catch (Exception ex)
                    {
                        string eee = ex.Message;
                    }
                    finally
                    {
                        if (sr != null)
                        {
                            sr.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                string eee = ex.Message;
            }
            return string.Empty;

        }

        public static string teamfound(string verify)
        {
            string postData = string.Format("verify={0}",verify);
            string url = "http://www.kaixin001.com/parking/teamfound.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/teamfound.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Value;
                    return Regex.Replace(Common.FilterHtml(matchcontent), "(\n|\t|\r)", "").Replace("</div", string.Empty);
                    //string re1 = "<strong>(?<message>.*?)</strong>";
                    //return Regex.Match(matchcontent, re1).Result("${message}").Replace("<br />", "\n");
                }
            }
            else if (response.StatusCode == HttpStatusCode.OK)
            {
                StreamReader sr = null;
                try
                {
                    sr = new StreamReader(response.GetResponseStream(), hp.encoding);
                    string ResStr1 = sr.ReadToEnd();
                    string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                    RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);
                    if (Regex.IsMatch(ResStr1, re, options))
                    {
                        Match mc = Regex.Match(ResStr1, re, options);
                        string matchcontent = mc.Value;
                        string re1 = "<strong>\\s*(?<message>.*?)。";
                        if (Regex.IsMatch(matchcontent, re1, options))
                            return Regex.Match(matchcontent, re1, options).Result("${message}");
                        else
                            return Common.FilterHtml(matchcontent);
                    }
                    else
                        return string.Empty;

                }
                catch (Exception ex)
                {
                    string eee = ex.Message;
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();
                    }
                }
            }
            return string.Empty;
        }

        public static string teambegin(string verify, int teamuid, int teamid)
        {
            string postData = string.Format("verify={0}&teamuid={1}&teamid={2}",
                verify, teamuid.ToString(), teamid.ToString());
            string url = "http://www.kaixin001.com/parking/teambegin.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/teambegin.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Value;
                    return Regex.Replace(Common.FilterHtml(matchcontent), "(\n|\t|\r)", "").Replace("</div", string.Empty);
                    //string re1 = "<strong>(?<message>.*?)</strong>";
                    //return Regex.Match(matchcontent, re1).Result("${message}").Replace("<br />", "\n");
                }
            }
            else if (response.StatusCode == HttpStatusCode.OK)
            {
                StreamReader sr = null;
                try
                {
                    sr = new StreamReader(response.GetResponseStream(), hp.encoding);
                    string ResStr1 = sr.ReadToEnd();
                    string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                    RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);
                    if (Regex.IsMatch(ResStr1, re, options))
                    {
                        Match mc = Regex.Match(ResStr1, re, options);
                        string matchcontent = mc.Value;
                        string re1 = "<strong>(?<message>.*?)</strong>";
                        return Regex.Match(matchcontent, re1, options).Result("${message}").Replace("<br />", "\n");
                    }
                    else
                        return string.Empty;

                }
                catch (Exception ex)
                {
                    string eee = ex.Message;
                }
                finally
                {
                    if (sr != null)
                    {
                        sr.Close();
                    }
                }
            }
            return string.Empty;
        }

        //发起比赛
        public static string MatchOrigin(string verify, int teamid,int lineid, int maxteamnum)
        {
            string postData = string.Format("verify={0}&teamid={1}&lineid={2}&teamid={3}",
                verify, teamid.ToString(), lineid.ToString(), maxteamnum.ToString());
            string url = "http://www.kaixin001.com/parking/matchorigin.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/matchorigin.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Value;
                    return Regex.Replace(Common.FilterHtml(matchcontent), "(\n|\t|\r)", "").Replace("</div", string.Empty);
                    //string re1 = "<strong>(?<message>.*?)</strong>";
                    //return Regex.Match(matchcontent, re1).Result("${message}").Replace("<br />", "\n");
                }
            }
            return string.Empty;
        }

        //解散车队
        public static string TeamDismiss(string verify, int teamid)
        {
            string postData = string.Format("verify={0}&teamid={1}",verify, teamid.ToString());
            string url = "http://www.kaixin001.com/parking/teamdismiss.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/teamdismiss.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Value;
                    return Regex.Replace(Common.FilterHtml(matchcontent), "(\n|\t|\r)", "").Replace("</div", string.Empty);
                    //string re1 = "<strong>(?<message>.*?)</strong>";
                    //return Regex.Match(matchcontent, re1).Result("${message}").Replace("<br />", "\n");
                }
            }
            return string.Empty;
        }

        //解除联合
        public static string TeamBreakUni(string verify, int teamid)
        {
            string postData = string.Format("verify={0}&teamid={1}", verify, teamid.ToString());
            string url = "http://www.kaixin001.com/parking/teambreakuni.php";
            HttpProc hp = new HttpProc(url, postData);
            HttpWebRequest request = hp.CreateRequest();
            request.AllowAutoRedirect = false;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if ((int)response.StatusCode == 302)
            {
                string location = response.Headers["location"];
                response.Close();
                url = string.Format("http://www.kaixin001.com/parking/teambreakuni.php{0}", location);
                hp = new HttpProc(url);
                string ResStr1 = hp.Proc();
                string re = "<div\\s+id=action\\d+\\s+style=\\\"display:block\\\">.*?</div>\\s+</div>\\s+</div";
                RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline) | RegexOptions.IgnoreCase);
                if (Regex.IsMatch(ResStr1, re, options))
                {
                    Match mc = Regex.Match(ResStr1, re, options);
                    string matchcontent = mc.Value;
                    return Regex.Replace(Common.FilterHtml(matchcontent), "(\n|\t|\r)", "").Replace("</div", string.Empty);
                    //string re1 = "<strong>(?<message>.*?)</strong>";
                    //return Regex.Match(matchcontent, re1).Result("${message}").Replace("<br />", "\n");
                }
            }
            return string.Empty;
        }

        public static string GetOilLink(int matchuid, int matchid)
        {
            return string.Format("http://www.kaixin001.com/app/app.php?aid=1040&url=match.php&matchuid={0}&matchid={1}",
                matchuid.ToString(), matchid.ToString());
        }

    }
}
